﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MovieRecorder.CApMRCRecorderSettingsViewModel
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.MovieRecorder;

public class CApMRCRecorderSettingsViewModel : INotifyPropertyChanged
{
  private CApMRCRecorderSettingsViewModel.AspectRatio m_aspectRatio;
  private const string DEFAULT_STANDARD_RESOLUTION = "FullHD (16:9)";
  private string m_customResolutionWidthErrorText;
  private string m_customResolutionHeigthErrorText;
  private CApMRCRecorderSettingsPreviewOverlayViewModel m_previewViewModel;

  public CApMRCRecorderSettingsViewModel(CApMRCRecorderSettings settings)
  {
    this.CreateFormatsList();
    this.CreateResolutionDictionary();
    this.Settings = settings;
    this.m_aspectRatio = new CApMRCRecorderSettingsViewModel.AspectRatio();
    this.m_previewViewModel = new CApMRCRecorderSettingsPreviewOverlayViewModel(this);
  }

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(string propertyName)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(propertyName));
  }

  protected CApMRCRecorderSettings Settings { get; set; }

  private List<Tuple<string, MovieResolution>> ResolutionTable { get; set; }

  public IList<KeyValuePair<string, string>> Formats { get; private set; }

  public IList<KeyValuePair<string, string>> ScreenCaptureOptions
  {
    get => this.Settings.ScreenCaptureOptions;
  }

  private Dictionary<string, IList<KeyValuePair<TxVideoCodec, string>>> CodecsPerFormat { get; set; }

  public string SelectedFormat
  {
    get => this.Settings.FileFormat;
    set
    {
      this.Settings.FileFormat = value;
      this.OnPropertyChanged(nameof (SelectedFormat));
      this.OnPropertyChanged("Codecs");
      this.OnPropertyChanged("Codec");
    }
  }

  public string SelectedScreenCaptureOption
  {
    get => this.Settings.SelectedScreenCaptureOption;
    set
    {
      this.Settings.SelectedScreenCaptureOption = value;
      this.OnPropertyChanged(nameof (SelectedScreenCaptureOption));
      this.UpdateRecorderIndicator();
    }
  }

  public virtual bool ScreenCaptureAreaEnabled => true;

  protected virtual void UpdateRecorderIndicator()
  {
    CApMRCCaptureAreaIndicatorManager.Instance.Update(this.Settings);
  }

  public string Destination
  {
    get => this.Settings.FileDestination;
    set
    {
      this.Settings.FileDestination = value;
      this.OnPropertyChanged(nameof (Destination));
    }
  }

  public virtual bool CanChooseDestination => true;

  public virtual bool CanChooseFileFormat => true;

  public CaptureArea ScreenCaptureArea
  {
    get => this.Settings.ScreenCaptureArea;
    set
    {
      this.Settings.ScreenCaptureArea = value;
      this.OnPropertyChanged(nameof (ScreenCaptureArea));
      this.OnPropertyChanged("TimeSource");
      this.OnPropertyChanged("TimeSourceEnabled");
      this.OnPropertyChanged("ChangeSimulationTimeInterval");
      this.OnPropertyChanged("ChangeSimulationTimeIntervalEnabled");
      this.UpdateResolution();
      this.UpdateRecorderIndicator();
      this.UpdateDisplayVisibilty();
    }
  }

  public bool MaintainRatio
  {
    get => this.m_aspectRatio.IsSet;
    set
    {
      if (value == this.m_aspectRatio.IsSet)
        return;
      if (value)
        this.m_aspectRatio.Set(this.ResolutionWidth, this.ResolutionHeight);
      else
        this.m_aspectRatio.Reset();
      this.OnPropertyChanged(nameof (MaintainRatio));
    }
  }

  public int SelectedResolution
  {
    get
    {
      if (this.Settings.SelectedResolution < 0)
        this.Settings.SelectedResolution = this.ResolutionTable.FindIndex((Predicate<Tuple<string, MovieResolution>>) (x => x.Item1 == "FullHD (16:9)"));
      return this.Settings.SelectedResolution;
    }
    set
    {
      if (this.Settings.SelectedResolution == value)
        return;
      this.Settings.SelectedResolution = value;
      this.OnPropertyChanged(nameof (SelectedResolution));
      this.OnPropertyChanged("SelectedResolutionName");
      this.UpdateResolution();
    }
  }

  public string SelectedResolutionName => this.ResolutionTable[this.SelectedResolution].Item1;

  public uint ResolutionWidthMax => 3840 /*0x0F00*/;

  public uint ResolutionHeightMax => 2160;

  public uint ResolutionWidthMin => 100;

  public uint ResolutionHeightMin => 100;

  public uint ResolutionWidth
  {
    get => this.Settings.ResolutionWidth;
    set
    {
      if ((int) this.Settings.ResolutionWidth == (int) value)
        return;
      uint? suggestedHeight;
      this.Settings.ResolutionWidth = this.FixResolutionWidth(value, out suggestedHeight);
      this.OnPropertyChanged(nameof (ResolutionWidth));
      this.MaintainResolutionHeight(suggestedHeight);
    }
  }

  public uint ResolutionHeight
  {
    get => this.Settings.ResolutionHeight;
    set
    {
      if ((int) this.Settings.ResolutionHeight == (int) value)
        return;
      uint? suggestedWidth;
      this.Settings.ResolutionHeight = this.FixResolutionHeight(value, out suggestedWidth);
      this.OnPropertyChanged(nameof (ResolutionHeight));
      this.MaintainResolutionWidth(suggestedWidth);
    }
  }

  public uint FramesRate
  {
    get => this.Settings.FrameRate;
    set
    {
      this.Settings.FrameRate = value;
      this.OnPropertyChanged(nameof (FramesRate));
      this.UpdateRecorderIndicator();
    }
  }

  public uint Compression
  {
    get => this.Settings.Compression;
    set
    {
      this.Settings.Compression = value;
      this.OnPropertyChanged(nameof (Compression));
    }
  }

  public TxVideoCodec Codec
  {
    get => this.Settings.Codec;
    set
    {
      this.Settings.Codec = value;
      this.OnPropertyChanged(nameof (Codec));
    }
  }

  public IList<KeyValuePair<TxVideoCodec, string>> Codecs
  {
    get => this.CodecsPerFormat[this.SelectedFormat];
  }

  public virtual bool ChangeSimulationTimeIntervalEnabled => this.TimeSource > 0;

  public bool ChangeSimulationTimeInterval
  {
    get => this.TimeSource != null && this.Settings.ChangeSimulationTimeInterval;
    set
    {
      this.Settings.ChangeSimulationTimeInterval = value;
      this.OnPropertyChanged(nameof (ChangeSimulationTimeInterval));
      this.UpdateRecorderIndicator();
    }
  }

  public virtual bool TimeSourceEnabled => this.ScreenCaptureArea != CaptureArea.Screen;

  public TxMovieTimeSource TimeSource
  {
    get
    {
      return this.ScreenCaptureArea == CaptureArea.Screen ? (TxMovieTimeSource) 0 : this.Settings.TimeSource;
    }
    set
    {
      this.Settings.TimeSource = value;
      this.OnPropertyChanged(nameof (TimeSource));
      this.OnPropertyChanged("ChangeSimulationTimeInterval");
      this.OnPropertyChanged("ChangeSimulationTimeIntervalEnabled");
      this.UpdateRecorderIndicator();
    }
  }

  public IList<KeyValuePair<TxMovieTimeSource, string>> TimeSources
  {
    get
    {
      return (IList<KeyValuePair<TxMovieTimeSource, string>>) new List<KeyValuePair<TxMovieTimeSource, string>>()
      {
        new KeyValuePair<TxMovieTimeSource, string>((TxMovieTimeSource) 0, MRCStringTable.MOVIE_RECORDER_SETTINGS_TIMESOURCE_REALTIME),
        new KeyValuePair<TxMovieTimeSource, string>((TxMovieTimeSource) 1, MRCStringTable.MOVIE_RECORDER_SETTINGS_TIMESOURCE_SIMTIME)
      };
    }
  }

  public string OverlayText
  {
    get => this.Settings.OverlayText;
    set
    {
      this.Settings.OverlayText = value;
      this.OnPropertyChanged(nameof (OverlayText));
      this.Preview.OverlayText = this.Settings.OverlayText;
    }
  }

  public short OverlayFontSize
  {
    get => this.Settings.OverlayFontSize;
    set
    {
      this.Settings.OverlayFontSize = value;
      this.OnPropertyChanged(nameof (OverlayFontSize));
      this.Preview.OverlayFontSize = this.Settings.OverlayFontSize;
    }
  }

  public System.Windows.Media.Color OverlayFontColor
  {
    get => this.Settings.OverlayFontColor;
    set
    {
      this.Settings.OverlayFontColor = value;
      this.OnPropertyChanged(nameof (OverlayFontColor));
      this.Preview.OverlayFontColor = (System.Windows.Media.Brush) System.Windows.Media.Brushes.White;
    }
  }

  public System.Windows.Media.Color OverlayFontShadowColor
  {
    get => this.Settings.OverlayFontShadowColor;
    set
    {
      this.Settings.OverlayFontShadowColor = value;
      this.OnPropertyChanged(nameof (OverlayFontShadowColor));
      this.Preview.OverlayFontShadowColor = this.Settings.OverlayFontShadowColor;
    }
  }

  public string OverlayImagePath
  {
    get
    {
      return this.Settings.OverlayImagePath != string.Empty ? this.Settings.OverlayImagePath : "pack://application:,,,/DnProcessSimulateCommands;component/MovieRecorder/res/add.png";
    }
    set
    {
      this.Settings.OverlayImagePath = value;
      try
      {
        this.Settings.OverlayImageWidth = 0U;
        this.Settings.OverlayImageHeight = 0U;
        using (Image image = Image.FromFile(this.Settings.OverlayImagePath))
        {
          if (image != null)
          {
            this.Settings.OverlayImageWidth = (uint) image.Width;
            this.Settings.OverlayImageHeight = (uint) image.Height;
          }
        }
      }
      catch
      {
        this.Settings.OverlayImagePath = string.Empty;
      }
      this.OnPropertyChanged(nameof (OverlayImagePath));
      this.OnPropertyChanged("AllowEditOverlayImage");
      this.OverlayImageSizePercent = this.FixImagePercentSize(this.OverlayImageSizePercent);
      this.Preview.OverlayImagePath = this.Settings.OverlayImagePath;
    }
  }

  public ImageSource OverlayAddImageIcon
  {
    get => TxImageManager.GetImageByKey("Edit").ConvertTo<ImageSource>(new System.Drawing.Size());
  }

  public ImageSource OverlayRemoveImageIcon
  {
    get => TxImageManager.GetImageByKey("DisapproveRed").ConvertTo<ImageSource>(new System.Drawing.Size());
  }

  public bool ValidSizeImage(uint size)
  {
    bool flag = true;
    if (this.AllowEditOverlayImage && (int) this.FixImagePercentSize(size) != (int) size)
      flag = false;
    return flag;
  }

  private uint FixImagePercentSize(uint size)
  {
    uint num1 = size;
    Screen applicationFrameScreen = CApMRCRecorderUtilities.GetApplicationFrameScreen();
    double num2 = (double) this.OverlayImageWidth.Percent(size);
    int num3 = applicationFrameScreen.WorkingArea.Width.Percent(60U);
    if (num2 > (double) num3)
    {
      uint num4 = (uint) ((double) (num3 * 100) / (double) this.OverlayImageWidth);
      if (num4 < num1)
        num1 = num4;
    }
    double num5 = (double) this.OverlayImageHeight.Percent(size);
    int num6 = applicationFrameScreen.WorkingArea.Height.Percent(60U);
    if (num5 > (double) num6)
    {
      uint num7 = (uint) ((double) (num6 * 100) / (double) this.OverlayImageHeight);
      if (num7 < num1)
        num1 = num7;
    }
    return num1;
  }

  public uint OverlayImageWidth => this.Settings.OverlayImageWidth;

  public uint OverlayImageHeight => this.Settings.OverlayImageHeight;

  public uint OverlayImageSizePercent
  {
    get => this.Settings.OverlayImageSizePercent;
    set
    {
      if ((int) this.OverlayImageSizePercent == (int) value)
        return;
      this.Settings.OverlayImageSizePercent = value;
      this.OnPropertyChanged(nameof (OverlayImageSizePercent));
      this.Preview.UpdateImage();
    }
  }

  public uint OverlayImageTransparency
  {
    get => this.Settings.OverlayImageTransparency;
    set
    {
      if ((int) this.OverlayImageTransparency == (int) value)
        return;
      this.Settings.OverlayImageTransparency = value;
      this.OnPropertyChanged(nameof (OverlayImageTransparency));
      this.Preview.UpdateImage();
    }
  }

  public bool AllowEditOverlayImage => this.Settings.OverlayImagePath != string.Empty;

  public TxMovieOverlayPosition OverlayPosition
  {
    get => this.Settings.OverlayPosition;
    set
    {
      this.Settings.OverlayPosition = value;
      this.OnPropertyChanged(nameof (OverlayPosition));
      this.Preview.UpdatePosition();
    }
  }

  public IList<KeyValuePair<TxMovieOverlayPosition, string>> OverlayPositions
  {
    get
    {
      return (IList<KeyValuePair<TxMovieOverlayPosition, string>>) new List<KeyValuePair<TxMovieOverlayPosition, string>>()
      {
        new KeyValuePair<TxMovieOverlayPosition, string>((TxMovieOverlayPosition) 0, MRCStringTable.MOVIE_RECORDER_OVERLAY_POSITION_TOP_LEFT),
        new KeyValuePair<TxMovieOverlayPosition, string>((TxMovieOverlayPosition) 1, MRCStringTable.MOVIE_RECORDER_OVERLAY_POSITION_TOP_CENTER),
        new KeyValuePair<TxMovieOverlayPosition, string>((TxMovieOverlayPosition) 2, MRCStringTable.MOVIE_RECORDER_OVERLAY_POSITION_TOP_RIGHT),
        new KeyValuePair<TxMovieOverlayPosition, string>((TxMovieOverlayPosition) 3, MRCStringTable.MOVIE_RECORDER_OVERLAY_POSITION_MIDDLE_LEFT),
        new KeyValuePair<TxMovieOverlayPosition, string>((TxMovieOverlayPosition) 4, MRCStringTable.MOVIE_RECORDER_OVERLAY_POSITION_MIDDLE_CENTER),
        new KeyValuePair<TxMovieOverlayPosition, string>((TxMovieOverlayPosition) 5, MRCStringTable.MOVIE_RECORDER_OVERLAY_POSITION_MIDDLE_RIGHT),
        new KeyValuePair<TxMovieOverlayPosition, string>((TxMovieOverlayPosition) 6, MRCStringTable.MOVIE_RECORDER_OVERLAY_POSITION_BOTTOM_LEFT),
        new KeyValuePair<TxMovieOverlayPosition, string>((TxMovieOverlayPosition) 7, MRCStringTable.MOVIE_RECORDER_OVERLAY_POSITION_BOTTOM_CENTER),
        new KeyValuePair<TxMovieOverlayPosition, string>((TxMovieOverlayPosition) 8, MRCStringTable.MOVIE_RECORDER_OVERLAY_POSITION_BOTTOM_RIGHT)
      };
    }
  }

  public IList<KeyValuePair<string, string>> OverlayVariables
  {
    get => (IList<KeyValuePair<string, string>>) TxMovieTextOverlay.Variables;
  }

  public CApMRCRecorderSettingsPreviewOverlayViewModel Preview => this.m_previewViewModel;

  public bool ShowNavigationCube
  {
    get => this.Settings.SceneSettings.ShowNavigationCube;
    set
    {
      this.Settings.SceneSettings.ShowNavigationCube = value;
      this.Settings.SceneSettings.ShowNavigationFrame = value;
      this.OnPropertyChanged(nameof (ShowNavigationCube));
    }
  }

  public bool ShowWorkingFrame
  {
    get => this.Settings.SceneSettings.ShowWorkingFrame;
    set
    {
      this.Settings.SceneSettings.ShowWorkingFrame = value;
      this.OnPropertyChanged(nameof (ShowWorkingFrame));
    }
  }

  public bool ShowOperations
  {
    get => this.Settings.SceneSettings.ShowOperations;
    set
    {
      this.Settings.SceneSettings.ShowOperations = value;
      this.OnPropertyChanged(nameof (ShowOperations));
    }
  }

  public bool Show2DObjects
  {
    get => this.Settings.SceneSettings.Show2DObjects;
    set
    {
      this.Settings.SceneSettings.Show2DObjects = value;
      this.OnPropertyChanged(nameof (Show2DObjects));
    }
  }

  public bool EnablePartCulling
  {
    get => this.Settings.SceneSettings.EnablePartCulling;
    set
    {
      this.Settings.SceneSettings.EnablePartCulling = value;
      this.OnPropertyChanged(nameof (EnablePartCulling));
    }
  }

  public bool ShowGraphicViewerToolbar
  {
    get
    {
      return this.ScreenCaptureArea == CaptureArea.Screen && this.Settings.SceneSettings.ShowGraphicViewerToolbar;
    }
    set
    {
      this.Settings.SceneSettings.ShowGraphicViewerToolbar = value;
      this.OnPropertyChanged(nameof (ShowGraphicViewerToolbar));
    }
  }

  public bool ShowSimulationSlider
  {
    get
    {
      return this.ScreenCaptureArea == CaptureArea.Screen && this.Settings.SceneSettings.ShowSimulationSlider;
    }
    set
    {
      this.Settings.SceneSettings.ShowSimulationSlider = value;
      this.OnPropertyChanged(nameof (ShowSimulationSlider));
    }
  }

  public bool ShowGraphicViewerToolbarEnabled => this.ScreenCaptureArea == CaptureArea.Screen;

  public bool ShowSimulationSliderEnabled => this.ScreenCaptureArea == CaptureArea.Screen;

  public Visibility ShowSimulationSliderVisibility => Visibility.Visible;

  private void UpdateDisplayVisibilty()
  {
    this.Settings.SceneSettings.HideGraphicViewerControlsAllowed = this.ScreenCaptureArea == CaptureArea.Screen;
    this.OnPropertyChanged("ShowGraphicViewerToolbar");
    this.OnPropertyChanged("ShowSimulationSlider");
    this.OnPropertyChanged("ShowGraphicViewerToolbarEnabled");
    this.OnPropertyChanged("ShowSimulationSliderEnabled");
  }

  public virtual bool CaptureAreaExpanded
  {
    get => this.Settings.ExpanderSettings.CaptureAreaExpanded;
    set
    {
      this.Settings.ExpanderSettings.CaptureAreaExpanded = value;
      this.OnPropertyChanged(nameof (CaptureAreaExpanded));
    }
  }

  public virtual bool CaptureAreaEnabled
  {
    get => this.Settings.ExpanderSettings.CaptureAreaEnabled;
    set
    {
      this.Settings.ExpanderSettings.CaptureAreaEnabled = value;
      this.OnPropertyChanged(nameof (CaptureAreaEnabled));
    }
  }

  public virtual bool TimingExpanded
  {
    get => this.Settings.ExpanderSettings.TimingExpanded;
    set
    {
      this.Settings.ExpanderSettings.TimingExpanded = value;
      this.OnPropertyChanged(nameof (TimingExpanded));
    }
  }

  public virtual bool TimingEnabled
  {
    get => this.Settings.ExpanderSettings.TimingEnabled;
    set
    {
      this.Settings.ExpanderSettings.TimingEnabled = value;
      this.OnPropertyChanged(nameof (TimingEnabled));
    }
  }

  public virtual bool CompressionExpanded
  {
    get => this.Settings.ExpanderSettings.CompressionExpanded;
    set
    {
      this.Settings.ExpanderSettings.CompressionExpanded = value;
      this.OnPropertyChanged(nameof (CompressionExpanded));
    }
  }

  public virtual bool CompressionEnabled
  {
    get => this.Settings.ExpanderSettings.CompressionEnabled;
    set
    {
      this.Settings.ExpanderSettings.CompressionEnabled = value;
      this.OnPropertyChanged(nameof (CompressionEnabled));
    }
  }

  public virtual bool OverlayExpanded
  {
    get => this.Settings.ExpanderSettings.OverlayExpanded;
    set
    {
      this.Settings.ExpanderSettings.OverlayExpanded = value;
      this.OnPropertyChanged(nameof (OverlayExpanded));
    }
  }

  public virtual bool OverlayEnabled
  {
    get => this.Settings.ExpanderSettings.OverlayEnabled;
    set
    {
      this.Settings.ExpanderSettings.OverlayEnabled = value;
      this.OnPropertyChanged(nameof (OverlayEnabled));
    }
  }

  public virtual bool VisibilityOptionsExpanded
  {
    get => this.Settings.ExpanderSettings.VisibilityOptionsExpanded;
    set
    {
      this.Settings.ExpanderSettings.VisibilityOptionsExpanded = value;
      this.OnPropertyChanged(nameof (VisibilityOptionsExpanded));
    }
  }

  public virtual bool VisibilityOptionsEnabled
  {
    get => this.Settings.ExpanderSettings.VisibilityOptionsEnabled;
    set
    {
      this.Settings.ExpanderSettings.VisibilityOptionsEnabled = value;
      this.OnPropertyChanged(nameof (VisibilityOptionsEnabled));
    }
  }

  public string CustomResolutionWidthErrorText
  {
    get => this.m_customResolutionWidthErrorText;
    set
    {
      if (!(this.m_customResolutionWidthErrorText != value))
        return;
      this.m_customResolutionWidthErrorText = value;
      this.OnPropertyChanged(nameof (CustomResolutionWidthErrorText));
    }
  }

  public string CustomResolutionHeigthErrorText
  {
    get => this.m_customResolutionHeigthErrorText;
    set
    {
      if (!(this.m_customResolutionHeigthErrorText != value))
        return;
      this.m_customResolutionHeigthErrorText = value;
      this.OnPropertyChanged(nameof (CustomResolutionHeigthErrorText));
    }
  }

  public bool ValidResoulutionWidth(uint width)
  {
    bool flag = true;
    if (this.ScreenCaptureArea == CaptureArea.GraphicViewerCustom)
    {
      this.CustomResolutionHeigthErrorText = string.Empty;
      flag = (int) this.ResolutionWidth != (int) this.FixResolutionWidth(width, out uint? _);
      if (!flag)
        this.CustomResolutionWidthErrorText = string.Format(MRCStringTable.MOVIE_RECORDER_SETTINGS_VALIDATE_HEIGHT, (object) this.ResolutionWidthMin, (object) this.ResolutionWidthMax);
    }
    return flag;
  }

  public bool ValidResoulutionHeight(uint height)
  {
    bool flag = true;
    if (this.ScreenCaptureArea == CaptureArea.GraphicViewerCustom)
    {
      this.CustomResolutionWidthErrorText = string.Empty;
      flag = (int) this.ResolutionHeight != (int) this.FixResolutionHeight(height, out uint? _);
      if (!flag)
        this.CustomResolutionHeigthErrorText = string.Format(MRCStringTable.MOVIE_RECORDER_SETTINGS_VALIDATE_WIDTH, (object) this.ResolutionHeightMin, (object) this.ResolutionHeightMax);
    }
    return flag;
  }

  private uint FixResolutionSize(
    uint value,
    uint currentValue,
    Func<uint, uint> AspectRatioConvertor,
    Func<int, bool> ValueInResolutionRange,
    Func<int, bool> RelativeValueInResolutionRange,
    out uint? suggestedRelativeValue)
  {
    bool flag1 = true;
    uint num1 = value;
    suggestedRelativeValue = new uint?();
    if (this.ScreenCaptureArea == CaptureArea.GraphicViewerCustom)
    {
      uint num2 = num1 % 4U;
      switch (num2)
      {
        case 0:
          flag1 = ValueInResolutionRange((int) num1);
          if (flag1 && this.MaintainRatio)
          {
            uint num3 = 0;
            int num4 = 0;
            bool flag2 = true;
            while (flag2)
            {
              num1 = (uint) ((ulong) num1 + (ulong) num4);
              num3 = AspectRatioConvertor(num1);
              flag2 = num3 % 4U > 0U;
              if (flag2)
              {
                num4 = currentValue > num1 ? -4 : 4;
                flag1 = ValueInResolutionRange((int) num1 + num4);
                flag2 = flag1;
              }
            }
            if (!flag1)
            {
              flag1 = true;
              num1 = currentValue;
              break;
            }
            flag1 = RelativeValueInResolutionRange((int) num3);
            if (!flag1)
            {
              flag1 = true;
              num1 = currentValue;
              break;
            }
            suggestedRelativeValue = new uint?(num3);
            break;
          }
          break;
        case 1:
        case 2:
          num1 -= num2;
          goto case 0;
        default:
          num1 += 4U - num2;
          goto case 0;
      }
    }
    return flag1 ? num1 : value;
  }

  private uint FixResolutionWidth(uint width, out uint? suggestedHeight)
  {
    return this.FixResolutionSize(width, this.ResolutionWidth, new Func<uint, uint>(this.m_aspectRatio.GetDenuminator), (Func<int, bool>) (value => (long) value >= (long) this.ResolutionWidthMin && (long) value <= (long) this.ResolutionWidthMax), (Func<int, bool>) (value => (long) value >= (long) this.ResolutionHeightMin && (long) value <= (long) this.ResolutionHeightMax), out suggestedHeight);
  }

  private uint FixResolutionHeight(uint height, out uint? suggestedWidth)
  {
    return this.FixResolutionSize(height, this.ResolutionHeight, new Func<uint, uint>(this.m_aspectRatio.GetNuminator), (Func<int, bool>) (value => (long) value >= (long) this.ResolutionHeightMin && (long) value <= (long) this.ResolutionHeightMax), (Func<int, bool>) (value => (long) value >= (long) this.ResolutionWidthMin && (long) value <= (long) this.ResolutionWidthMax), out suggestedWidth);
  }

  private void MaintainResolutionHeight(uint? height)
  {
    if (!this.MaintainRatio || this.ScreenCaptureArea != CaptureArea.GraphicViewerCustom)
      return;
    this.Settings.ResolutionHeight = height.HasValue ? height.Value : this.m_aspectRatio.GetDenuminator(this.ResolutionWidth);
    this.OnPropertyChanged("ResolutionHeight");
  }

  private void MaintainResolutionWidth(uint? width)
  {
    if (!this.MaintainRatio || this.ScreenCaptureArea != CaptureArea.GraphicViewerCustom)
      return;
    this.Settings.ResolutionWidth = width.HasValue ? width.Value : this.m_aspectRatio.GetNuminator(this.ResolutionHeight);
    this.OnPropertyChanged("ResolutionWidth");
  }

  private void UpdateResolution()
  {
    if (this.ScreenCaptureArea == CaptureArea.GraphicViewerCustom)
      return;
    this.MaintainRatio = false;
    this.Settings.ResolutionWidth = this.ResolutionTable[this.SelectedResolution].Item2.Width;
    this.Settings.ResolutionHeight = this.ResolutionTable[this.SelectedResolution].Item2.Height;
    this.OnPropertyChanged("ResolutionWidth");
    this.OnPropertyChanged("ResolutionHeight");
  }

  private void CreateFormatsList()
  {
    this.Formats = (IList<KeyValuePair<string, string>>) new List<KeyValuePair<string, string>>();
    this.Formats.Add(new KeyValuePair<string, string>("mp4", "MPEG-4 Video (*.mp4)"));
    this.Formats.Add(new KeyValuePair<string, string>("wmv", "Windows Media Video (*.wmv)"));
    this.Formats.Add(new KeyValuePair<string, string>("avi", "Audio Video Interleaved (*.avi)"));
    this.Formats.Add(new KeyValuePair<string, string>("mkv", "Matroska Video (*.mkv)"));
    this.Formats.Add(new KeyValuePair<string, string>("webm", "WebM Video (*.webm)"));
    this.Formats.Add(new KeyValuePair<string, string>("ogv", "Ogg Video (*.ogv)"));
    this.CodecsPerFormat = new Dictionary<string, IList<KeyValuePair<TxVideoCodec, string>>>();
    this.CodecsPerFormat.Add("wmv", (IList<KeyValuePair<TxVideoCodec, string>>) new List<KeyValuePair<TxVideoCodec, string>>());
    this.CodecsPerFormat.Add("avi", (IList<KeyValuePair<TxVideoCodec, string>>) new List<KeyValuePair<TxVideoCodec, string>>());
    this.CodecsPerFormat.Add("mp4", (IList<KeyValuePair<TxVideoCodec, string>>) new List<KeyValuePair<TxVideoCodec, string>>());
    this.CodecsPerFormat.Add("mkv", (IList<KeyValuePair<TxVideoCodec, string>>) new List<KeyValuePair<TxVideoCodec, string>>());
    this.CodecsPerFormat.Add("webm", (IList<KeyValuePair<TxVideoCodec, string>>) new List<KeyValuePair<TxVideoCodec, string>>());
    this.CodecsPerFormat.Add("ogv", (IList<KeyValuePair<TxVideoCodec, string>>) new List<KeyValuePair<TxVideoCodec, string>>());
    List<TxVideoCodec> supportedCodecs = TxMovieRecordingSettings.GetSupportedCodecs();
    if (supportedCodecs.Contains((TxVideoCodec) 13))
    {
      KeyValuePair<TxVideoCodec, string> keyValuePair = new KeyValuePair<TxVideoCodec, string>((TxVideoCodec) 13, "MPEG-4 Part 2");
      this.CodecsPerFormat["avi"].Add(keyValuePair);
      this.CodecsPerFormat["wmv"].Add(keyValuePair);
      this.CodecsPerFormat["mp4"].Add(keyValuePair);
      this.CodecsPerFormat["mkv"].Add(keyValuePair);
    }
    if (supportedCodecs.Contains((TxVideoCodec) 28))
    {
      KeyValuePair<TxVideoCodec, string> keyValuePair = new KeyValuePair<TxVideoCodec, string>((TxVideoCodec) 28, "MPEG-4 AVC (H264)");
      this.CodecsPerFormat["avi"].Add(keyValuePair);
      this.CodecsPerFormat["wmv"].Add(keyValuePair);
      this.CodecsPerFormat["mp4"].Add(keyValuePair);
      this.CodecsPerFormat["mkv"].Add(keyValuePair);
    }
    if (supportedCodecs.Contains((TxVideoCodec) 174))
    {
      KeyValuePair<TxVideoCodec, string> keyValuePair = new KeyValuePair<TxVideoCodec, string>((TxVideoCodec) 174, "MPEG-H HEVC (H265)");
      this.CodecsPerFormat["mp4"].Add(keyValuePair);
      this.CodecsPerFormat["mkv"].Add(keyValuePair);
    }
    if (supportedCodecs.Contains((TxVideoCodec) 167))
      this.CodecsPerFormat["webm"].Add(new KeyValuePair<TxVideoCodec, string>((TxVideoCodec) 167, "VP9"));
    if (!supportedCodecs.Contains((TxVideoCodec) 30))
      return;
    this.CodecsPerFormat["ogv"].Add(new KeyValuePair<TxVideoCodec, string>((TxVideoCodec) 30, "Theora"));
  }

  private void CreateResolutionDictionary()
  {
    this.ResolutionTable = new List<Tuple<string, MovieResolution>>();
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("QVGA (4:3)", new MovieResolution(320U, 240U /*0xF0*/)));
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("VGA (4:3)", new MovieResolution(640U, 480U)));
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("DVD (4:3)", new MovieResolution(720U, 576U)));
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("SVGA (4:3)", new MovieResolution(800U, 600U)));
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("XGA (4:3)", new MovieResolution(1024U /*0x0400*/, 768U /*0x0300*/)));
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("HD (16:9)", new MovieResolution(1280U /*0x0500*/, 720U)));
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("SXGA (4:3)", new MovieResolution(1280U /*0x0500*/, 1024U /*0x0400*/)));
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("FullHD (16:9)", new MovieResolution(1920U, 1080U)));
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("WUXGA (16:10)", new MovieResolution(1920U, 1200U)));
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("QuadHD (16:9)", new MovieResolution(2560U /*0x0A00*/, 1440U)));
    this.ResolutionTable.Add(new Tuple<string, MovieResolution>("4K UltraHD (16:9)", new MovieResolution(3840U /*0x0F00*/, 2160U)));
  }

  private class AspectRatio
  {
    private uint Numinator;
    private uint Denuminator;
    private uint gcd;

    public AspectRatio()
    {
      this.Numinator = 0U;
      this.Denuminator = 0U;
      this.gcd = 0U;
    }

    public bool IsSet => this.Numinator != 0U && this.Denuminator > 0U;

    public void Reset()
    {
      this.Numinator = 0U;
      this.Denuminator = 0U;
      this.gcd = 0U;
    }

    public void Set(uint num, uint denum)
    {
      this.gcd = this.GCD(num, denum);
      this.Numinator = num / this.gcd;
      this.Denuminator = denum / this.gcd;
    }

    public uint GetNuminator(uint denum)
    {
      uint numinator = this.GetNuminator(denum, CApMRCRecorderSettingsViewModel.AspectRatio.Rounding.ToNearest);
      if (numinator % 4U != 0U)
        numinator = this.GetNuminator(denum, CApMRCRecorderSettingsViewModel.AspectRatio.Rounding.AwayFromNearest);
      return numinator;
    }

    private uint GetNuminator(
      uint denum,
      CApMRCRecorderSettingsViewModel.AspectRatio.Rounding rounding)
    {
      Decimal numinator = (Decimal) (denum * this.Numinator) / (Decimal) this.Denuminator;
      if (rounding == CApMRCRecorderSettingsViewModel.AspectRatio.Rounding.ToNearest)
        return (uint) numinator;
      Decimal num = 0.5M;
      return (uint) (numinator + num);
    }

    public uint GetDenuminator(uint num)
    {
      uint denuminator = this.GetDenuminator(num, CApMRCRecorderSettingsViewModel.AspectRatio.Rounding.ToNearest);
      if (denuminator % 4U != 0U)
        denuminator = this.GetDenuminator(num, CApMRCRecorderSettingsViewModel.AspectRatio.Rounding.AwayFromNearest);
      return denuminator;
    }

    private uint GetDenuminator(
      uint num,
      CApMRCRecorderSettingsViewModel.AspectRatio.Rounding rounding)
    {
      Decimal denuminator = (Decimal) (num * this.Denuminator) / (Decimal) this.Numinator;
      if (rounding == CApMRCRecorderSettingsViewModel.AspectRatio.Rounding.ToNearest)
        return (uint) denuminator;
      Decimal num1 = 0.5M;
      return (uint) (denuminator + num1);
    }

    private uint GCD(uint a, uint b)
    {
      uint num;
      for (; b != 0U; b = num)
      {
        num = a % b;
        a = b;
      }
      return a;
    }

    private enum Rounding
    {
      ToNearest,
      AwayFromNearest,
    }
  }
}
